// 引入定义仓库的函数
import { defineStore } from 'pinia';
// 引入商品分类相关的接口函数
import {getCategory1ListApi, getCategory2ListApi, getCategory3ListApi} from '@/api/product/category'
// 引入分类相关的接口类型
import {CategoryIdObjModel, CategoryModel, Numberable, CategoryListModel} from '@/api/product/model/categoryModel'
// 定义state状态数据的类型
export interface categoryStateModel extends CategoryIdObjModel {
  category1List: CategoryModel[],
  category2List: CategoryModel[],
  category3List: CategoryModel[]
}
export const useCategoryStore = defineStore({
  id: 'category',
  // 状态数据
  state: ():categoryStateModel => ({
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
  }),
  // actions
  actions: {
    // 获取一级分类
    async getCategory1List() {
      this.category1List = await getCategory1ListApi()
    },
    // 获取二级分类
    async getCategory2List(category1Id: number) {
      this.category1Id = category1Id
      this.category2List = await getCategory2ListApi(category1Id)
      // 重置以下数据
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
    },
    // 获取三级分类
    async getCategory3List(category2Id: number) {
      this.category2Id = category2Id
      this.category3List = await getCategory3ListApi(category2Id)
      // 重置以下数据
      this.category3Id = undefined
    },

  },
  // 计算属性
  getters: {
    _category1Id():Numberable {
      return this.category1Id as number
    },
    _category2Id():Numberable {
      return this.category2Id as number
    },
    _category3Id():Numberable {
      return this.category3Id as number
    },
    _category1List(): CategoryListModel {
      return this.category1List
    },
    _category2List(): CategoryListModel {
      return this.category2List
    },
    _category3List(): CategoryListModel {
      return this.category3List
    }
  }
})